package com.ks.app.admin.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.util.Pager;
import com.ks.app.navigate.util.JsonUtil;


@ParentPackage("struts-default")
@InterceptorRef("defaultStack")

@Results( {
	@Result(name = "editlog", location = "/WEB-INF/logjsp/editlog.jsp"),
	@Result(name = "listlog", location = "/WEB-INF/logjsp/listlog.jsp")
})
public class KsLogAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	@Resource(name="ksoperatelogService")
	private KsoperatelogService logService;
	
	
	/**
	 * 客户端-异常日志保存
	 * @return
	 */
	@Action(value="ks_add_log")
	public String addLog(){

		String content = request.getParameter("cont")==null?"":request.getParameter("cont").trim();
		if(!"".equals(content)){
			
			KsOperaterLog log = new KsOperaterLog();
			log.setContent(content);
			log.setLogtime(new Date());
			log.setLogtype(2);             // 1 操作日志，2客户端日志
			log.setOperater("client");
			log.setStatus(1);              // 2 已处理，  1 未处理
			logService.save(log);
			JsonUtil.outJson("{\"ret\":\"success\"}", response);
			
		}else{
			
			JsonUtil.outJson("{\"error\":\"日志内容为空！\"}", response);
			
		}
		return null;
	}
	
	/**
	 * 日志详细页面
	 * @return
	 */
	@Action(value="edit_log")
	public String editlog(){
		
		long  id =  (request.getParameter("id")==null || "".equals(request.getParameter("id")))?0l:Long.parseLong(request.getParameter("id"));
		int pno  =  (request.getParameter("pno")==null || "".equals(request.getParameter("pno")))?1:Integer.parseInt(request.getParameter("pno"));
		
		int status = (request.getParameter("status") == null || "".equals(request.getParameter("status")))?0:Integer.parseInt(request.getParameter("status"));
		int logtype = (request.getParameter("logtype") == null || "".equals(request.getParameter("logtype")))?0:Integer.parseInt(request.getParameter("logtype"));
		String opename = request.getParameter("opename")     == null?"":request.getParameter("opename").trim();
		String logtime = request.getParameter("logtime")     == null?"":request.getParameter("logtime").trim();
		
		if(id !=0){
			KsOperaterLog log = logService.findLogById(id);
			request.setAttribute("log", log);
		}
		
		request.setAttribute("status", status);
		request.setAttribute("opename", opename);
		request.setAttribute("logtype", logtype);
		request.setAttribute("logtime", logtime);
		request.setAttribute("pno", pno);
		
		return "editlog";
	}
	

	/**
	 * 后台----获取日志列表数据
	 * @return
	 */
	@Action(value="ks_log_grid")
	public String findLogList(){
		
		try {

			int pno        = (request.getParameter("pno")== null || "".equals(request.getParameter("pno")))?1:Integer.parseInt(request.getParameter("pno"));
			int status     = (request.getParameter("status")== null || "".equals(request.getParameter("status")))?0:Integer.parseInt(request.getParameter("status"));
			int logtype    = (request.getParameter("logtype") == null || "".equals(request.getParameter("logtype")))?0:Integer.parseInt(request.getParameter("logtype"));
			String opename = request.getParameter("opename") == null?"":request.getParameter("opename").trim();
			String logtime = request.getParameter("logtime") == null?"":request.getParameter("logtime").trim();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			KsOperaterLog klog = new KsOperaterLog();
			klog.setStatus(status);
			if(!"".equals(logtime)){
				klog.setLogtime(sdf.parse(logtime));
				request.setAttribute("logtime", logtime);
			}
			klog.setLogtype(logtype);
			klog.setOpename(opename);
			
			Pager pager = new Pager();
			pager.setCntOfPage(20);
			pager.setCurPage(pno);
			
			List<KsOperaterLog> list = logService.findKsOperaterLogList(opename,logtime,logtype, status, pager);
			
			if(pno>1 && (list==null || list.size()==0)){
				
				pager.setCurPage(pno-1);
				list = logService.findKsOperaterLogList(opename,logtime,logtype, status, pager);
				
			}else if(pno==1 && (list==null || list.size()==0)){
				pager.setCurPage(0);
			}

			request.setAttribute("list", list);
			request.setAttribute("PAGER", pager);
			request.setAttribute("log", klog);
	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "listlog";
	}
	
	/**
	 * 后台-----审核异常日志
	 * @return
	 */
	@Action(value="ks_check_log")
	public String checkLog(){
			
		String logids = request.getParameter("id")==null?"":request.getParameter("id");
		if(!"".equals(logids)){
			logService.checkLog(logids);	
		}
		return findLogList();
	}

	
}
